<?php

namespace App\Http\Controllers\Api\v1;

use App\Http\Controllers\Controller;
use App\Logic\GaoLogic;
use App\Models\Resources;
use Illuminate\Http\Request;
use App\common;
use App\Logic\FangLogic;


/**
 * Undocumented class
 * 房源 @zf
 */
class FangController extends Controller
{
    /**
     * Undocumented function
     *  房源列表
     * @return void
     */
    public function fangList(Request $request){
        try{
            if($request->method()!= 'GET')throw new \Exception('请求方式不正确');
            $fangList = FangLogic::fangList();
            return common::success(20000, $fangList, '加载成功');
        } catch (\Exception  $e) {
            return common::fail(2001, '', $e->getMessage());
        }
    }

    /**
     * 房源搜索
     */
    public function fangSearch(Request $request){
        try{
            if($request->method()!= 'GET')throw new \Exception('请求方式不正确');
            $name=$request->get('name');
            $xiaoqu=$request->get('xiaoqu');
            $shi=$request->get('shi');
            $wei=$request->get('wei');
            $ting=$request->get('ting');
            $paginate=$request->get('paginate');
            $fangList=FangLogic::fangSearch($name,$xiaoqu,$shi,$wei,$ting,$paginate);
            return common::success(20000,$fangList,'加载成功');
        }catch(\Exception  $e){
            return common::fail(2001,'',$e->getMessage());
        }
    }

    /**
     * Undocumented function
     *  房源详情
     * @return void
     */
    public function fangDetail(Request $request){
        try{
            if($request->method()!= 'GET')throw new \Exception('请求方式不正确');
            $id=$request->get('id');
            $request->validate([
                'id' => 'required',
            ]);
            $adminDetail = FangLogic::fangDetail($id);
            return common::success(20000, $adminDetail, '加载成功');
        } catch (\Exception  $e) {
            return common::fail(2001, '', $e->getMessage());
        }
    }

    /**
     * Undocumented function
     *  编辑房源信息
     * @return void
     */
    public function fangSave(Request $request){
        try{
            if($request->method()!= 'POST')throw new \Exception('请求方式不正确');
            $updata=$request->post();
            $request->validate([
                "fang_name" => "required",
                "fang_xiaoqu" => "required",
                "fang_addr" => "required",
                "pic" => "required",
                "fang_config" => "required",
                "fang_desn" => "required",
                "fang_body" => "required",

            ]);
            $save = FangLogic::fangSave($updata);
            return common::success(20000, $save, '加载成功');

        } catch (\Exception  $e) {
            return common::fail(2001, '', $e->getMessage());
        }
    }

    /**
     * 房源删除
     */
    public function fangDel(Request $request)
    {
        try {
            if($request->method()!= 'GET')throw new \Exception('请求方式不正确');
            $id = $request->get('id');
            $adminDel = FangLogic::fangDel($id);
            return common::success(20000, $adminDel, '查询成功');
        } catch (\Exception  $e) {

            return common::fail(2001, '', $e->getMessage());
        }
    }

    /**
     * 房源列表s
     * @yuankai
     */
    public function list(Request $request)
    {
            try {
                if ($request->method() == 'GET'){
                    $result = FangLogic::list();
                    return common::success('200',$result,'数据列表');
                }
                else{
                     throw new \Exception('请求方式不正确');
                }
            }catch (\Exception $exception){
                return common::fail('','',$exception->getMessage());
            }
    }

    /**
     * 房源分类搜索
     * @yuankai
     */
    public function search(Request $request)
    {
        try {
            if ($request->method() == 'GET')
            {
                $category = $request->get('category');
                $result = FangLogic::search($category);
                if ($result){
                    return common::success('200',$result,'分类查询成功');
                }
            }else{
                throw new \Exception('请求方式不正确');
            }
        } catch (\Exception $exception) {
            return common::fail('', '', $exception->getMessage());
        }
    }

    /**
     * @param Request $request
     * @return array|false|string
     * 附近房源
     */
    public function nearby(Request $request)
    {
        try {
            if ($request->method() == 'GET'){
                $neighborhood = $request->get('neighborhood');
                $result = FangLogic::nearby_search($neighborhood);
                if ($result){
                    return common::success('200',$result,'房源附近查询成功');
                }
            }else{
                throw new \Exception('请求方式不正确');
            }
        } catch (\Exception $exception) {
            return common::fail('', '', $exception->getMessage());
        }
    }

    /**
     *价格区间查询
     * @yuankai
     */
    public function price(Request $request)
    {
        try {
            if ($request->method() == 'GET'){
                $price = $request->get('price');
                $result = FangLogic::price($price);
                if ($result){
                    return common::success('200',$result,'价格区间查询成功');
                }
            }else{
                throw new \Exception('请求方式不正确');
            }
        } catch (\Exception $exception) {
            return common::fail('', '', $exception->getMessage());
        }
    }

    /**
     * 房源户型查询
     * @yuankai
     */
    public function house_type(Request $request)
    {
        try {
            if ($request->method() == 'GET')
            {
                $bedrooms = $request->get('bedrooms');
                $area = $request->get('area');
                $bathrooms = $request->get('bathrooms');
                $result = FangLogic::house_type($bedrooms,$area,$bathrooms);
                if ($result){
                    return common::success('200',$result,'房源户型');
                }else{
                    return common::success('','','没有该户型');
                }
            }else{
                throw new \Exception('请求方式不正确');
            }
        } catch (\Exception $exception) {
            return common::fail('', '', $exception->getMessage());
        }
    }

    /**
     * 房源多条件搜索
     * @yuankai
     */
    public function more(Request $request)
    {
            try {
                if ($request->method() == 'GET')
                {
                    $category = $request->get('category');
                    $rent_type = $request->get('rent_type');
                    $result = FangLogic::more($category,$rent_type);
                    if ($result){
                        return common::success('200',$result,'多条件搜索成功');
                    }
                }else{
                    throw new \Exception('请求方式不正确');
                }
            }catch (\Exception $exception){
                return common::fail('','',$exception->getMessage());
            }
    }

    /**
     * 房源的经纬度转换
     * @yuankai
     */
    public function map(Request $request)
    {
            try {
                if ($request->method() == 'GET')
                {
                    $city = $request->get('city');
                    $res = FangLogic::getCity($city);
                    if ($res){
                        return common::success('200',$res,'经纬度');
                    }
                }else{
                    throw new \Exception('请求方式不正确');
                }
            }catch (\Exception $exception){
                return common::fail('','',$exception->getMessage());
            }
    }

}
